package com.shsc.seasar.search.util;

import java.util.HashSet;
import java.util.Iterator;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.search.ui.ISearchPageContainer;
import org.eclipse.search.ui.text.FileTextSearchScope;
import org.eclipse.ui.IWorkingSet;

public class SearchScopeUtil {

	
	/**
	 * @param fContainer
	 * @param extensions
	 * @return
	 */
	public static FileTextSearchScope createTextSearchScope(ISearchPageContainer fContainer, String[] extensions) {
		
		// Setup search scope
        switch (fContainer.getSelectedScope()) {
            case ISearchPageContainer.WORKSPACE_SCOPE:
                return FileTextSearchScope.newWorkspaceScope(extensions, false);
            case ISearchPageContainer.SELECTION_SCOPE:
                return getSelectedResourcesScope(fContainer, extensions);
            case ISearchPageContainer.SELECTED_PROJECTS_SCOPE:
                return getEnclosingProjectScope(fContainer, extensions);
            case ISearchPageContainer.WORKING_SET_SCOPE:
                IWorkingSet[] workingSets= fContainer.getSelectedWorkingSets();
                return FileTextSearchScope.newSearchScope(workingSets, extensions, false);
            default:
                // unknown scope
                return FileTextSearchScope.newWorkspaceScope(extensions, false);
        }
	}
	
	/**
	 * @param fContainer
	 * @param extensions
	 * @return
	 */
	private static FileTextSearchScope getEnclosingProjectScope(ISearchPageContainer fContainer, String[] extensions) {
        String[] enclosingProjectName= fContainer.getSelectedProjectNames();
        if (enclosingProjectName == null) {
            return FileTextSearchScope.newWorkspaceScope(extensions, false);
        }
        
        IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
        IResource[] res= new IResource[enclosingProjectName.length];
        for (int i= 0; i < res.length; i++) {
            res[i]= root.getProject(enclosingProjectName[i]);
        }
        
        return FileTextSearchScope.newSearchScope(res, extensions, false);
    }
	
	/**
	 * @param fContainer
	 * @param extensions
	 * @return
	 */
	private static FileTextSearchScope getSelectedResourcesScope(ISearchPageContainer fContainer, String[] extensions) {
        HashSet<IResource> resources= new HashSet<IResource>();
        ISelection sel= fContainer.getSelection();
        if (sel instanceof IStructuredSelection && !sel.isEmpty()) {
            Iterator iter= ((IStructuredSelection) sel).iterator();
            while (iter.hasNext()) {
                Object curr= iter.next();
                if (curr instanceof IWorkingSet) {
                    IWorkingSet workingSet= (IWorkingSet) curr;
                    if (workingSet.isAggregateWorkingSet() && workingSet.isEmpty()) {
                        return FileTextSearchScope.newWorkspaceScope(extensions, false);
                    }
                    IAdaptable[] elements= workingSet.getElements();
                    for (int i= 0; i < elements.length; i++) {
                        IResource resource= (IResource)elements[i].getAdapter(IResource.class);
                        if (resource != null && resource.isAccessible()) {
                            resources.add(resource);
                        }
                    }
                } else if (curr instanceof IAdaptable) {
                    IResource resource= (IResource) ((IAdaptable)curr).getAdapter(IResource.class);
                    if (resource != null && resource.isAccessible()) {
                        resources.add(resource);
                    }
                }
            }
        }
        IResource[] arr= (IResource[]) resources.toArray(new IResource[resources.size()]);
        return FileTextSearchScope.newSearchScope(arr, extensions, false);
    }
}
